package com.ljy.utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.URI;

public class Doc2 {
    public Document getDocument(String url, String code){
        return getDocument(url,null,code);
    }
    public Document getDocument(URI uri, String code){
        return getDocument(null,uri,code);
    }
    private Document getDocument(String url, URI uri,String code){
        // 1.创建httpclient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2.创建httpget实例
        HttpGet httpGet;
        if (url!=null&&url.length()>1) httpGet = new HttpGet(url);
        else if (uri!=null)httpGet = new HttpGet(uri);
        else {
            System.out.println("Doc2中出错");
            return null;
        }
        // 3.配置
        // 3.1模拟浏览器
        httpGet.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
        // 3.2代理+超时
        //使用代理 IP
//      HttpHost proxy = new HttpHost("47.101.195.97",3128);
        RequestConfig config = RequestConfig.custom()//.setProxy(proxy)
                //设置连接超时
                .setConnectTimeout(10000) // 设置连接超时时间 10秒钟
                .setSocketTimeout(10000) // 设置读取超时时间10秒钟
                .build();
        httpGet.setConfig(config);

        //响应
        CloseableHttpResponse response = null;
        Document document=null;
        try {
            // 使用HttpClient发起请求，获取response
            response = httpClient.execute(httpGet);
            // 解析响应
            if(response.getStatusLine().getStatusCode() == 200){
                String content = EntityUtils.toString(response.getEntity(), code);
                document= Jsoup.parse(content);
            }
        } catch (IOException e) {
            System.out.println("抓取失败,"+e.getMessage());
        } finally {
            // 关闭response
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return document;
    }
}
